using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using MercuryParticleEngine.Emitters;

namespace BumbleGame
{
    class Flower
    {
        private int screenheight, points, coins, pollenCloudColor, pollenColor;
        public int color; // 0=blue 1=red 2=yellow
        private bool transition, isFemale, isPenny, isNickel, isDime, isQuarter;
        public bool pollenated, pollenGone, occupied, isVisible;                
        private AnimatedTexture pollenCloud, penny, nickel, dime, quarter;
        public AnimatedTexture flower, stem;
        public HitBox2D BigHitBox;

              
        public Flower(int height, int nColor, bool female)
        {
            screenheight = height;  
            pollenated = false;
            coins = 1;
            points = 100;
            occupied = false;
            pollenGone = false;
            isVisible = true;
            isFemale = female;
            transition = false;
            color = nColor;
            pollenCloudColor = 0;
            pollenColor = 0;
            pollenCloud = new AnimatedTexture();
            pollenCloud.SpriteScale = 0;
            penny = new AnimatedTexture();
            nickel = new AnimatedTexture();
            dime = new AnimatedTexture();
            quarter = new AnimatedTexture();
            flower = new AnimatedTexture();
            stem = new AnimatedTexture();
            isPenny = false;
            isNickel = false;
            isDime = false;
            isQuarter = false;
        }

        public void Load(GraphicsDeviceManager graphics, ContentManager content, string flowerImageID)
        {
            flower.Load(graphics.GraphicsDevice, content, flowerImageID, 8, 1, 12, 0, 7, true);
            stem.Load(graphics.GraphicsDevice, content, "Images/Flowers/stem", 1, 1, 1, 0, 0, false);            
            stem.SpritePosition.Y = (0 - ((stem.aTexture.Height / stem.framesTall) / 2));
            flower.SpritePosition.X = stem.SpritePosition.X;
            flower.SpritePosition.Y = stem.SpritePosition.Y - 90;
            if (isFemale)
            {
                pollenCloud.Load(graphics.GraphicsDevice, content, "Images/pollenCloud", 8, 2, 9, 0, 15, false);
                //animation tester
                //pollenCloud.Load(graphics.GraphicsDevice, content, "Images/frame_reference", 8, 3, 5, 0, 15, true);
                penny.Load(graphics.GraphicsDevice, content, "Images/copper_3f_128", 4, 1, 8, 0, 2, true);
                nickel.Load(graphics.GraphicsDevice, content, "Images/silver_3f_128x128", 4, 1, 8, 0, 2, true);
                dime.Load(graphics.GraphicsDevice, content, "Images/gold_3f_128", 4, 1, 8, 0, 2, true);
                quarter.Load(graphics.GraphicsDevice, content, "Images/iceicebaby_3f_128", 4, 1, 8, 0, 2, true);           
                flower.SetHitBoxOffset(80); 
               
                //coins are too big
                penny.SpriteScale = 0.5f;
                nickel.SpriteScale = 0.5f;
                dime.SpriteScale = 0.5f;
                quarter.SpriteScale = 0.5f;

            }
        }
        
        //Scroll the flower at entry time
        public void UpdateFlower(float scrollY, float elapsed)
        {
            if (flower.SpritePosition.Y >= (screenheight + ((flower.aTexture.Height / flower.framesTall) / 2)))
            { isVisible = false; }
            else
            {
                flower.UpdateFrame(elapsed);
                stem.UpdateFrame(elapsed);
                stem.SpritePosition.Y += scrollY;
                flower.SpritePosition.X = stem.SpritePosition.X;
                flower.SpritePosition.Y = stem.SpritePosition.Y - 90;
            }
        }

        public void UpdateFlower(float scrollY, float elapsed, List<Vector2> tempListLeft, List<Vector2> tempListRight,
            SoundBank sfxBank, Bumblepig bumblepig, int pollenColorRight, int pollenColorLeft, ref int nStreakCount, ref bool justPollenated, int nConfidenceMode, ref int nLastPollenCloudColor, ref FlowerTotals totalFlowers)
        {
            BigHitBox = new HitBox2D(new Vector2((flower.SpritePosition.X - ((flower.aTexture.Width / flower.framesWide) / 8)),
                                               (flower.SpritePosition.Y - ((flower.aTexture.Height / flower.framesTall) / 8))),
                                   new Vector2((flower.SpritePosition.X + ((flower.aTexture.Width / flower.framesWide) / 8)),
                                              ((flower.SpritePosition.Y + ((flower.aTexture.Height / flower.framesTall) / 8)))));
            if (flower.SpritePosition.Y >= (screenheight + ((flower.aTexture.Height / flower.framesTall) / 2)))
            { isVisible = false; }
            else
            {
                flower.UpdateFrame(elapsed);
                stem.UpdateFrame(elapsed);
                stem.SpritePosition.Y += scrollY;
                flower.SpritePosition.X = stem.SpritePosition.X;
                flower.SpritePosition.Y = stem.SpritePosition.Y - 100;
                penny.SpritePosition = flower.SpritePosition;
                nickel.SpritePosition = flower.SpritePosition;
                dime.SpritePosition = flower.SpritePosition;
                quarter.SpritePosition = flower.SpritePosition;
                pollenCloud.SpritePosition = flower.SpritePosition;
            }
            if (isVisible && !pollenated)
            {
                foreach (Vector2 tempVector in tempListRight)
                {
                    if (flower.HitBox2.Contains(tempVector))
                    {
                        pollenated = true;
                        transition = true;
                        pollenColor = pollenColorRight;
                        break;
                    }
                }

                if (!pollenated)
                {
                    foreach (Vector2 tempVector in tempListLeft)
                    {
                        if (flower.HitBox2.Contains(tempVector))
                        {
                            pollenated = true;                            
                            transition = true;
                            pollenColor = pollenColorLeft; 
                            break;
                        }
                    }
                }
                if (pollenated == true)
                {                    
                    //pollen cloud color: 0 = blue, 1 = red, 2 = yellow, 3 = purple, 4 = green, 5 = orange
                    if (color == 0)
                    {
                        switch (pollenColor)
                        {
                            case 0:
                                pollenCloudColor = 0;
                                totalFlowers.blue++;
                                pollenCloud.tint = Color.Blue;
                                break;
                            case 1:
                                pollenCloudColor = 3;
                                totalFlowers.purple++;
                                pollenCloud.tint = Color.Purple;
                                break;
                            case 2:
                                pollenCloudColor = 4;
                                totalFlowers.green++;
                                pollenCloud.tint = Color.Green;
                                break;
                        }
                    }
                    if (color == 1)
                    {
                        switch (pollenColor)
                        {
                            case 0:
                                pollenCloudColor = 3;
                                totalFlowers.purple++;
                                pollenCloud.tint = Color.Purple;
                                break;
                            case 1:
                                pollenCloudColor = 1;
                                totalFlowers.red++;
                                pollenCloud.tint = Color.Red;
                                break;
                            case 2:
                                pollenCloudColor = 5;
                                totalFlowers.orange++;
                                pollenCloud.tint = Color.Orange;
                                break;
                        }
                    }
                    if (color == 2)
                    {
                        switch (pollenColor)
                        {
                            case 0:
                                pollenCloudColor = 4;
                                totalFlowers.green++;
                                pollenCloud.tint = Color.Green;
                                break;
                            case 1:
                                pollenCloudColor = 5;
                                totalFlowers.orange++;
                                pollenCloud.tint = Color.Orange;
                                break;
                            case 2:
                                pollenCloudColor = 2;
                                totalFlowers.yellow++;
                                pollenCloud.tint = Color.Yellow;
                                break;
                        }
                    }

                    if (pollenCloudColor == nLastPollenCloudColor)
                    { nStreakCount += 1; }
                    else
                    { nStreakCount = 1; }
                    
                    nLastPollenCloudColor = pollenCloudColor;
                    justPollenated = true;

                    if (nConfidenceMode == 0)
                    { coins = 1; }
                    if (nConfidenceMode == 1)
                    { coins = 5; }
                    if (nConfidenceMode == 2)
                    { coins = 10; }
                    if (nConfidenceMode == 3)
                    { coins = 25; }

                    bumblepig.score += points;
                    bumblepig.coins += coins;
                    sfxBank.PlayCue("PennyPollenation");

                }
            }
            if (isVisible && pollenated)
            {
                if (transition)
                {
                    switch (nConfidenceMode)
                    {
                        case 0:
                            isPenny = true;
                            transition = false;                            
                            break;
                        case 1:
                            isNickel = true;
                            transition = false;
                            break;
                        case 2:
                            isDime = true;
                            transition = false;
                            break;
                        case 3:
                            isQuarter = true;
                            transition = false;
                            break;
                    }
                }
                if (isPenny)
                {
                    penny.UpdateFrame(elapsed);
                    if (penny.FrameRight == penny.framesWide - 1)
                    {
                        penny.Reset();
                        isPenny = false;
                    }
                    pollenCloud.SpriteScale += 0.01f;
                    if (pollenCloud.SpriteScale > 0.5f)
                    {
                        pollenCloud.SpriteScale = 0.5f;
                    }
                }
                if (isNickel)
                {
                    nickel.UpdateFrame(elapsed);
                    if (nickel.FrameRight == nickel.framesWide - 1)
                    {
                        nickel.Reset();
                        isNickel = false;
                    }
                    pollenCloud.SpriteScale += 0.01f;
                    if (pollenCloud.SpriteScale > 0.5f)
                    {
                        pollenCloud.SpriteScale = 0.5f;
                    }
                }
                if (isDime)
                {
                    dime.UpdateFrame(elapsed);
                    if (dime.FrameRight == dime.framesWide - 1)
                    {
                        dime.Reset();
                        isDime = false;
                    }
                    pollenCloud.SpriteScale += 0.01f;
                    if (pollenCloud.SpriteScale > 0.5f)
                    {
                        pollenCloud.SpriteScale = 0.5f;
                    }
                }
                if (isQuarter)
                {
                    quarter.UpdateFrame(elapsed);
                    if (quarter.FrameRight == quarter.framesWide - 1)
                    {
                        quarter.Reset();
                        isQuarter = false;
                    }
                    pollenCloud.SpriteScale += 0.01f;
                    if (pollenCloud.SpriteScale > 0.5f)
                    {
                        pollenCloud.SpriteScale = 0.5f;
                    }
                }
                pollenCloud.UpdateFrame(elapsed);
            }
        }       
        

        public void DrawFlower(SpriteBatch Batch)
        {
            //Draw flower at entryTime
            if (isVisible)
            {
                //DrawFrame(Batch, FrameRight, FrameDown);                
                flower.DrawFrame(Batch);                
                if (pollenated)
                {
                    pollenCloud.DrawFrame(Batch);
                    if (isPenny)
                        { penny.DrawFrame(Batch); }
                    if (isNickel)
                        { nickel.DrawFrame(Batch); }
                    if (isDime)
                        { dime.DrawFrame(Batch); }
                    if (isQuarter)
                        { quarter.DrawFrame(Batch); }
                }
            }
        }
        public void DrawStem(SpriteBatch Batch) //draw stems separately so they can be below the flowers
        {
            stem.DrawFrame(Batch);
        }
    }
}
